const dbPool = require("../database");


// 添加商品路由回调
exports.addGoods = function (req, res) {
  if (!req.file || req.file.fieldname !== "image") return res.cc("商品图片不能为空！");
  const sqlStr = "select * from goods where name = ?";
  dbPool.query(sqlStr, req.body.name, (err, results) => {
    if (err) return res.cc(err);
    if (results.length === 1) return res.cc("该商品已存在，不能重复添加！");
    const sqlStr = "insert into goods set ?";
    dbPool.query(sqlStr, { ...req.body, image: "/assets/" + req.file.filename, time: Date.now() }, (err, results) => {
      if (err) return res.cc(err);
      if (results.affectedRows !== 1) return res.cc("商品添加失败！");
      res.cc("商品添加成功!", 1);
    });
  });
}